<?php
namespace app\admin\controller;
use think\Controller;
class Conf extends Controller
{
	private $obj;
	public function _initialize()
	{	
		$this->obj=model('Conf');
	}
	public function conflst()
	{
		$confRes=$this->obj->select();

		return $this->fetch('',array(
					'confRes'=>$confRes,
			));
	}	
	/**
	 * 配置列表
	 * @return [type] [description]
	 */
	public function lst()
	{

		$confRes=$this->obj->getConf();
		return $this->fetch('',array(
				'confRes'=>$confRes,
			));
	}
	/**
	 * 增加配置
	 */
	public function add()
	{
		if(request()->isPost())
		{
			$data=input('post.');

			$validate=validate('Conf');
			if(!$validate->scene('add')->check($data))
			{
				$this->error($validate->getError());
			}
			$res=$this->obj->addConf($data);
			if($res)
			{
				$this->success('增加成功',url('lst'));
			}
			else
			{
				$this->error('增加失败');
			}
		}
		return $this->fetch();
	}	
	/**
	 * 编辑修改配置数据
	 * @return [type] [description]
	 */
	public function edit()
	{
		$id=input('id');
		$confs=$this->obj->get($id);

		if(request()->isPost())
		{
			$data=input('post.');
			$validate=validate('Conf');
			if(!$validate->scene('edit')->check($data))
			{
				$this->error($validate->getError());
			}
			$res=$this->obj->save($data,['id'=>$data['id']]);
			if($res)
			{
				$this->success('更新成功','lst');
			}
			else
			{
				$this->error('更新失败');
			}
		}
		return $this->fetch('',array(
			'confs'=>$confs,
		));
	}
	public function del()
	{
		$id=input('id');
		$res=$this->obj->delConf($id);
		if($res)
		{
			$this->success('删除成功',url('lst'));
		}
		else
		{
			$this->error('删除失败');
		}
	}
}